/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	using DataProvider;

	/// <summary>
	/// This control displays a summary of all of the sections that have links that need moderation.
	/// </summary>
	public class ResLinkModeration : ResLinkBaseTemplatedWebControl
	{

		#region Child Controls
			private RepeaterPlusNone _ModerationSummary = null;
		#endregion

		public ResLinkModeration()
		{
		}

		protected override void AttachChildControls()
		{
			_ModerationSummary = FindControl("ModerationSummary") as RepeaterPlusNone;
			_ModerationSummary.ItemDataBound += new RepeaterItemEventHandler(_ModerationSummary_ItemDataBound);
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if(!Page.IsPostBack)
			{
				DataBind();
			}
		}

		public override void DataBind()
		{
			base.DataBind();

			ResLinkDataProvider  rldp = ResLinkDataProvider.Instance();

			ArrayList items = rldp.GetModerationSummary();

			//If this user is not a global moderator then we need to check to see if 
			//any of the sections listed are not available for moderation
			if ( !CSContext.Current.User.IsModerator &&  !ExtendedUserRoles.IsLinkAdministrator)
			{
				for(int i=0;i<items.Count;i++)
				{
					ResLinkDataProvider.ModSummary sum = items[i] as ResLinkDataProvider.ModSummary;

					//Get section and check it permissions
					if (!ResLinkPermission.LightAccessCheck( ResLinkSection.GetSection(sum.SectionID) , ResLinkPermissionEnum.Moderate , CurrentUser))
					{
						items.RemoveAt(i);
						i--;//Correct the index
					}
				}
			}

			_ModerationSummary.DataSource = items;
			_ModerationSummary.DataBind();
		}

		private void _ModerationSummary_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem)
			{
				Label linkCount = e.Item.FindControl("LinkCount") as Label;
				Label linkCommentCount = e.Item.FindControl("LinkCommentCount") as Label;
				HyperLink name = e.Item.FindControl("SectionName") as HyperLink;
				
				ResLinkDataProvider.ModSummary sum = e.Item.DataItem as ResLinkDataProvider.ModSummary;
				linkCount.Text = sum.LinkTotal.ToString();
				linkCommentCount.Text = sum.LinkCommentTotal.ToString();
				name.Text = sum.Name;

				//Now work out the hyper link to the section to moderate
				name.NavigateUrl = ResLinkUrls.Instance().ModerationSection(sum.SectionID);
			}
		}
	}
}
